//
//  BNNewFeatureViewController.m
//  项目
//
//  Created by Lee on 15/9/27.
//  Copyright (c) 2015年 ww. All rights reserved.
//

#import "BNNewFeatureViewController.h"
#import "UIView+Extension.h"
#define BNNewfeatureCount 3
#import "PlayerButton.h"
#import "WWTabBarController.h"

@interface BNNewFeatureViewController () <UIScrollViewDelegate>

@property (nonatomic, retain) UIPageControl *pageController;
@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation BNNewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //1.创建一个scrollview 显示所有新特性图片
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    //2.添加图片到scrollView
    CGFloat scrollH = scrollView.height;
    CGFloat scrollW = scrollView.width;
    for (int i = 0; i < BNNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height  = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        NSString *name = [NSString stringWithFormat:@"%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        if (i == BNNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }

    }
    //3.设置scrollView相关属性
    scrollView.contentSize = CGSizeMake(BNNewfeatureCount * scrollView.width,0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    //4.添加UIPageController
    UIPageControl *pageController = [[UIPageControl alloc] init];
    pageController.numberOfPages = BNNewfeatureCount;
    pageController.centerX = scrollW / 2;
    pageController.centerY = scrollH - 55;
    pageController.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageController.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:pageController];
    self.pageController = pageController;
    [[UIApplication sharedApplication].windows.lastObject addSubview:[PlayerButton shareButton]];
//    [self.window addSubview:[PlayerButton shareButton]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    self.pageController.currentPage = (int)(page + 0.5);
    // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 开始应用
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = imageView.width * 0.5;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"开始应用" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}


- (void)startClick
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    [window addSubview:[PlayerButton shareButton]];
    WWTabBarController *tab = [[WWTabBarController alloc] init];
    [tab.tabBar NightWithType:UIViewColorTypeNormal];
    window.rootViewController = tab;
    [window addSubview:[PlayerButton shareButton]];
}

@end
